package com.hqx.gulimall.product.service.impl;

import com.hqx.common.constant.ProductConstant;
import com.hqx.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.hqx.gulimall.product.dao.AttrDao;
import com.hqx.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.hqx.gulimall.product.entity.AttrEntity;
import com.hqx.gulimall.product.vo.AttrGroupWithAttrRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqx.common.utils.PageUtils;
import com.hqx.common.utils.Query;

import com.hqx.gulimall.product.dao.AttrGroupDao;
import com.hqx.gulimall.product.entity.AttrGroupEntity;
import com.hqx.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Resource
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(key)) {
            queryWrapper.eq("attr_group_id", key).or().like("attr_group_name", key);
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id",catelogId);
        if (StringUtils.hasText(key)) {
            queryWrapper.and((obj) -> obj.eq("attr_group_id", key).or().like("attr_group_name", key));
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getRelationAttr(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id",attrGroupId));
        List<Long> attrIds =  attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        if(attrIds.size() == 0)
            return null;
        return attrDao.selectBatchIds(attrIds);
    }

    @Override
    public void removeRelationAttr(List<AttrAttrgroupRelationEntity> relationEntities) {
        if(relationEntities != null && relationEntities.size() != 0)
            attrAttrgroupRelationDao.deleteBatchRelationAttr(relationEntities);
    }

    @Override
    public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = this.getById(attrGroupId);
        if(attrGroupEntity == null)
            return null;
        //获取分类下所有分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",attrGroupEntity.getCatelogId()));
        List<Long> attrGroupIds = attrGroupEntities.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", attrGroupIds));
        List<Long>  attrIds = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id",attrGroupEntity.getCatelogId()).eq("attr_type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
        if(attrIds.size() != 0)
            queryWrapper.notIn("attr_id",attrIds);//查出分类下未被关联过的基本属性
        String key = (String)params.get("key");
        if(StringUtils.hasText(key))
            queryWrapper.and((obj) -> obj.eq("attr_id", key).or().like("attr_name", key));
        IPage<AttrEntity> page = attrDao.selectPage(new Query<AttrEntity>().getPage(params),queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrRespVo> getAttrGroupWithAttrByCatalogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));
       return attrGroupEntityList.stream().map(attrGroupEntity -> {
            AttrGroupWithAttrRespVo attrGroupWithAttrRespVo = new AttrGroupWithAttrRespVo();
            BeanUtils.copyProperties(attrGroupEntity,attrGroupWithAttrRespVo);
            List<AttrEntity> attrEntityList = getRelationAttr(attrGroupEntity.getAttrGroupId());
            attrGroupWithAttrRespVo.setAttrEntityList(attrEntityList);
            return attrGroupWithAttrRespVo;
        }).collect(Collectors.toList());
    }
}